package com.zyy.ui.utils;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zyy.ui.enums.EnumForDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * @ClassName: LogJsonSerialization
 * @Description: 转json格式工具类
 * @Author: zyy
 * @Date: 2020/04/28 23:07
 * @Version: 1.0
 */
public class LogJsonSerialization {
    private static LogJsonSerialization instance;
    private static Logger logger = LoggerFactory.getLogger(LogJsonSerialization.class);

    private LogJsonSerialization() {
    }

    public static LogJsonSerialization getInstance() {
        if (instance == null) {
            synchronized (LogJsonSerialization.class) {
                if (instance != null) {
                    return instance;
                }
                instance = new LogJsonSerialization();
            }
        }
        return instance;

    }

    public String toJSONString(Object message) {
        try {
            return getFormatJsonString(message);
        } catch (Exception e) {
            logger.error("log to json string error...", e);
        }
        return null;
    }

    private String getFormatJsonString(Object message) {
        SerializeWriter sw = new SerializeWriter();
        JSONSerializer jsonSerializer = new JSONSerializer(sw);

        jsonSerializer.setDateFormat(EnumForDateFormat.YYYY_MM_DD_HH_MM_SS.getDateFormat());

        jsonSerializer.config(SerializerFeature.WriteClassName, false);
        jsonSerializer.config(SerializerFeature.WriteDateUseDateFormat, true);
        jsonSerializer.config(SerializerFeature.WriteEnumUsingToString, true);
        jsonSerializer.config(SerializerFeature.WriteMapNullValue, true);
        jsonSerializer.config(SerializerFeature.WriteNullListAsEmpty, true);

        jsonSerializer.getValueFilters().add((obj, str, value) -> {
            if (value == null) {
                return null;
            }
            try {
                DecimalFormat df = new DecimalFormat("#0.0000#");
                if (value.getClass().equals(Double.class)) {
                    return df.format(((Double) value).doubleValue());
                } else if (value.getClass().equals(Long.class)) {
                    return df.format(((Long) value).longValue());
                } else if (value.getClass().equals(Short.class)) {
                    return df.format(((Short) value).shortValue());
                } else if (value.getClass().equals(BigDecimal.class)) {
                    return df.format(((BigDecimal) value).doubleValue());
                } else if (value.getClass().equals(double.class)) {
                    return df.format(value);
                } else if (value.getClass().equals(long.class)) {
                    return df.format(value);
                } else if (value.getClass().equals(short.class)) {
                    return df.format(value);
                } else {
                    return value;
                }
            } catch (Exception e) {
                logger.error("process error...", e);
            }
            return null;
        });
        jsonSerializer.write(message);
        return sw.toString();
    }
}
